package com.zsr.example.controller;


import com.alibaba.fastjson.JSONObject;
import com.zsr.example.config.annotation.OptLog;
import com.zsr.example.entity.EduChapter;
import com.zsr.example.service.EduChapterService;
import com.zsr.example.util.CommonUtil;
import com.zsr.example.vo.ChapterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zsr.example.util.constants.SysLogConstants.DELETE;
import static com.zsr.example.util.constants.SysLogConstants.UPDATE;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author zsr
 * @since 2021-11-15
 */
@RestController
@RequestMapping("/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;

    /**
     * 通过课程获取章节小节
     * @param courseId
     * @return
     */
    @GetMapping("getChapterSection/{courseId}")
    public JSONObject getChapterVideo(
            @PathVariable String courseId){
        List<ChapterVo> list= eduChapterService.getChapterVideoByCourseId(courseId);

        return CommonUtil.successJson(list);
    }

    //章节下有小节不可以删除
    //TODO  可能会做功能 删除章节下的所有小节
    @OptLog(optType = DELETE,optDesc = "删除章节")
    @DeleteMapping("{id}")
    public JSONObject removeById(
            @PathVariable String id){

        boolean result = eduChapterService.removeChapterById(id);

        return result ? CommonUtil.success(): CommonUtil.errorJson();
    }

    /**
     * 新增章节
     * @param chapter
     * @return
     */
    @PostMapping("saveChapter")
    public JSONObject saveChapter(
            @RequestBody EduChapter chapter){

        eduChapterService.save(chapter);
        return CommonUtil.success();
    }

    @GetMapping("{id}")
    public JSONObject getChapterById(
            @PathVariable String id){

        EduChapter chapter = eduChapterService.getById(id);
        return CommonUtil.successJson(chapter);
    }

    /**
     * 修改章节
     * @param chapter
     * @return
     */
    @OptLog(optType = UPDATE,optDesc = "修改章节")
    @PostMapping("upChapterDate")
    public JSONObject upChapterDate(
            @RequestBody EduChapter chapter){

        eduChapterService.updateById(chapter);
        return CommonUtil.success();
    }

}

